import { _decorator, AssetManager, Component, JsonAsset, Node, TextAsset } from 'cc';
import { Singleton } from './Singleton';
import { Logger } from '../log/Logger';
import { LoaderManager } from './LoaderManager';
import { LoaderBundle } from './LoaderBundle';



export class ConfigManager extends Singleton {

    protected init(): void {
        
    }
    //是否打印配置查找时间
    public debug:boolean=true;
 
    private _bundle_map:any={}
    private _config_map:any={}

    public loadBundle(path:string):Promise<AssetManager.Bundle>{
        return new Promise(resolve=>{
            LoaderManager.getInstance().loadBundle(path,null,null,(loader:LoaderBundle)=>{
                const bundle:AssetManager.Bundle=loader.bundle;
                ConfigManager.getInstance().addBundle(bundle)
                LoaderManager.getInstance().release(path)
                resolve(bundle); 
            })
        })
    }

    public addBundle(bundle:AssetManager.Bundle){
        if(bundle && !this._bundle_map[bundle.name]){
            this._bundle_map[bundle.name]=bundle
            let assetInfos = bundle["config"].assetInfos
            assetInfos?.forEach(element => {
                let path:string=element.path
                let jsonAsset:JsonAsset=bundle.get(path)
                if(jsonAsset){
                    this._config_map[path]=Object.assign({},jsonAsset.json)
                }
            });
            if(assetInfos["count"] > 0){
                bundle.releaseAll();
            }
        }
    }
    public removeBundle(name:string){
        if(this._bundle_map[name]){
            let bundle:AssetManager.Bundle = this._bundle_map[name]
            if(bundle){
                bundle.releaseAll();
                bundle=null
                delete this._bundle_map[name]
            }
        }
    }
    
    /** 
     * 获取指定的配置
     * @param name 
     * @param where 
     */
    getConfig<T extends {new()}>(bundleName:string,name:string,where?:any,type?:T):any{
        if(this.debug)Logger.log(`配置开始查找时间 > ${name}`)
        if( bundleName&& !this._config_map[name]){
            let bundle:AssetManager.Bundle = this._bundle_map[bundleName]
            if(bundle){
                let data:JsonAsset=bundle.get(name,JsonAsset)
                this._config_map[name]=Object.assign({},data.json)
                this.removeBundle(bundleName);
            }
        }
        let value:any=this._config_map[name]
        if(value && where){ 
            let itor_value:any;
            for(let key in value){  
                let has:boolean=true
                let itemValue:any=value[key] 
                for(let item_key in where){
                    if(itemValue[item_key] && itemValue[item_key] != where[item_key]){
                        has=false
                        break
                    }
                }
                if(has){
                    itor_value=itemValue
                    break
                }
            }
            if(type && itor_value){
                let new_value:T=new type();
                for(let key in new_value){
                    if(itor_value[key])new_value[key]=itor_value[key]
                }
                if(this.debug)Logger.log(`配置结束查找时间 > ${name}`)
                return new_value
            }else{
                if(this.debug)Logger.log(`配置结束查找时间 > ${name}`)
                return itor_value
            }
        }
        
    }
    /**
     * 获取指定条件的全部配置，如果wher是null则获取去全部表对应的数据
     * @param name 
     * @param where 
     */
    getConfigList<T extends {new()}>(bundleName:string,name:string,where?:any,type?:T):any[]{
        if(this.debug)Logger.log(`配置开始查找时间 > ${name}`)
        let item_list:any[]=[]
        if( bundleName&& !this._config_map[name]){
            let bundle:AssetManager.Bundle = this._bundle_map[bundleName]
            if(bundle){
                let data:JsonAsset=bundle.get(name,JsonAsset)
                this._config_map[name]=Object.assign({},data.json)
                this.removeBundle(bundleName);
            }
        }
        let value:any=this._config_map[name]
        if(value && where){
            let itor_value:any;
            for(let key in value){  
                let has:boolean=true
                let itemValue:any=value[key] 
                for(let item_key in where){
                    if(itemValue[item_key] && itemValue[item_key] != where[item_key]){
                        has=false
                        break
                    }
                }
                if(has){ 
                    itor_value=itemValue
                    let new_value:T=new type();
                    for(let key in new_value){
                        if(itor_value[key])new_value[key]=itor_value[key]
                    }
                    item_list.push(new_value)
                }
            }
        }
        if(this.debug)Logger.log(`配置结束查找时间 > ${name}`)
        return item_list
    }

}


